package org.apache.spark.examples

import org.apache.spark.sql.SparkSession

import scala.collection.mutable
import scala.util.Random

/**
  * 思路：
  * *
  *1.生成数据（from，to），为初试可达节点对数目(同时也是基本的节点跳转规则）
  * *
  *2.对数据需要做一次链接操作，（类似于一次矩阵乘）
  * *
  *3.将链接操作的结果提取成（from，to）形式，与当前的可达节点对做并集，得到最新的当前可达节点对数目
  * *
  *3.比较当前可达节点对的数量与上一轮节点对数量
  * *
  *4.若没有增加，则停止；否则，跳转至2继续执行
  */
object SparkTC {
  val numEdges = 200
  val numVertices = 100
  val rand = new Random(42)

  def main(args: Array[String]) {
    val spark = SparkSession
      .builder
      .appName("SparkTC")
      .getOrCreate()
    val slices = if (args.length > 0) args(0).toInt else 2
    var tc = spark.sparkContext.parallelize(generateGraph, slices).cache()

    // Linear transitive closure: each round grows paths by one edge,
    // by joining the graph's edges with the already-discovered paths.
    // e.g. join the path (y, z) from the TC with the edge (x, y) from
    // the graph to obtain the path (x, z).

    // Because join() joins on keys, the edges are stored in reversed order.
    val edges = tc.map(x => (x._2, x._1))

    // This join is iterated until a fixed point is reached.
    var oldCount = 0L
    var nextCount = tc.count()
    do {
      oldCount = nextCount
      // Perform the join, obtaining an RDD of (y, (z, x)) pairs,
      // then project the result to obtain the new (x, z) paths.
      tc = tc.union(tc.join(edges).map(x => (x._2._2, x._2._1))).distinct().cache()
      nextCount = tc.count()
    } while (nextCount != oldCount)

    println(s"TC has ${tc.count()} edges.")
    spark.stop()
  }

  def generateGraph: Seq[(Int, Int)] = {
    val edges: mutable.Set[(Int, Int)] = mutable.Set.empty
    while (edges.size < numEdges) {
      val from = rand.nextInt(numVertices)
      val to = rand.nextInt(numVertices)
      if (from != to) edges.+=((from, to))
    }
    edges.toSeq
  }
}

